package com.py.wemedia.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.py.model.admin.dtos.AdWmNewsDto;
import com.py.model.admin.dtos.NewsAuthDto;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.wemedia.pojos.WmNews;
import com.py.model.wemedia.pojos.WmUser;
import com.py.wemedia.service.AutoScanService;
import com.py.wemedia.service.WmNewsService;
import com.py.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName WmNewsClient
 * @Description TODO
 * @Author AXing
 * @Date 2022/11/17 17:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/news")
@Slf4j
public class WmNewsClient {
    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private AutoScanService autoScanService;

    @PostMapping("/newsList")
    public ResponseResult wmNewsList(@RequestBody NewsAuthDto dto){
        log.info("newsAuthDto---->{}", dto);
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        //自媒体文章id条件
        queryWrapper.eq(dto.getId() != null , WmNews::getId, dto.getId());
        //自媒体文章状态条件
        queryWrapper.eq(dto.getStatus() != null, WmNews::getStatus, dto.getStatus());
        //自媒体文章标题模糊查询
        queryWrapper.like(dto.getTitle() != "", WmNews::getTitle, dto.getTitle());
        wmNewsService.page(page, queryWrapper);
        List<WmNews> wmNewsList = page.getRecords();
        log.info("处理前records---->{}", wmNewsList);
        List<AdWmNewsDto> adWmNewsDtos = new ArrayList<>();
        if (page.getRecords() != null && page.getRecords().size() != 0){
            //添加作者名字
            adWmNewsDtos = wmNewsList.stream().map(wmNews -> {
                AdWmNewsDto adWmNewsDto = new AdWmNewsDto();
                BeanUtils.copyProperties(wmNews, adWmNewsDto);
                //查询自媒体用户姓名
                WmUser wmuser = wmUserService.getById(wmNews.getUserId());
                if (wmuser != null && wmuser.getName() != null){
                    adWmNewsDto.setAuthorName(wmuser.getName());
                }
                return adWmNewsDto;
            }).collect(Collectors.toList());
        }
        log.info("处理前records---->{}", adWmNewsDtos);
        return ResponseResult.okResult(adWmNewsDtos);
    }

    @PostMapping("/getDetail")
    public ResponseResult getDetail(@RequestParam Integer id){
        //通过id获取wmNews
        WmNews wmNews = wmNewsService.getById(id);
        AdWmNewsDto adWmNewsDto = new AdWmNewsDto();
        BeanUtils.copyProperties(wmNews, adWmNewsDto);
        //通过自媒体用户id得到自媒体实现类
        WmUser wmUser = wmUserService.getById(wmNews.getUserId());
        adWmNewsDto.setAuthorName(wmUser.getName());
        return ResponseResult.okResult(adWmNewsDto);
    }

    @PostMapping("/refusePass")
    public ResponseResult refusePass(@RequestBody NewsAuthDto dto){
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus((short)2);
        wmNews.setReason(dto.getMsg());
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(null);
    }

    @PostMapping("/pass")
    public ResponseResult pass(@RequestBody NewsAuthDto dto){
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getId());
        wmNews.setStatus((short)4);
        wmNews.setReason("审核成功");
        wmNewsService.updateById(wmNews);
        wmNews = wmNewsService.getById(wmNews.getId());
        autoScanService.save(wmNews);
        return ResponseResult.okResult(null);
    }
}
